package ar.com.angrymachine.hunter;

import java.util.Stack;

import ar.com.angrymachine.hunter.activities.CastActivity;
import ar.com.angrymachine.hunter.activities.RestActivity;
import ar.com.angrymachine.hunter.gameplay.HolySmitingSpell;
import ar.com.angrymachine.hunter.gameplay.Player;
import ar.com.angrymachine.hunter.locations.TownActivitySet;

public class Game
{
    public static Game current;
    public ActivitySet currentLevel;
    public ActivitySet townLevel;
    public Player player;
    private Activity runningActivity = null;
    private ActivitySet nextLocation = null;
    private Stack locations = new Stack();
    public boolean isOver = false;

    public Game()
    {
        player = new Player();
        player.gold = 200;
        current = this;
        // create initial town
        setCurrentLocation(new TownActivitySet());
        townLevel = currentLevel;
        // create default level
        //currentLevel = DungeonFactory.generateDungeon(DungeonFactory.HUB);
        // default user spells
        //player.activities.addActivity(new CastActivity(new ShatteringScream(1)));
        player.activities.addActivityInTail(new RestActivity());
        player.activities.addActivityInTail(new CastActivity(new HolySmitingSpell()));
    }
    
    // called when the game should pause the currently running
    // action because something happened
    public void resetActivity()
    {
        runningActivity = null;
    }
    
    public void setRunningActivity(Activity activity)
    {
        runningActivity = activity;
    }
    
    public Activity getRunningActivity()
    {
        return runningActivity;
    }
    
    public void run()
    {
    	if(isOver)
    	{
    		return;
    	}
        currentLevel.update(GameSettings.TIMER_RATE, runningActivity);
        player.activities.update(GameSettings.TIMER_RATE, runningActivity);
        if(nextLocation != null)
        {
        	resetActivity();
        	setCurrentLocation(nextLocation);
        	nextLocation = null;
        }
        if(player.isDead())
        {
        	isOver = true;
        	HunterApp.app.onGameOver();
        }
    }
    
    private void setCurrentLocation(ActivitySet location)
    {
    	currentLevel = location;
    	location.onEnter();
    }

	public void goToLocation(ActivitySet newLocation)
	{
		locations.push(currentLevel);
		nextLocation = newLocation;
	}
	
	public void goToPreviousLocation()
	{
		nextLocation = (ActivitySet) locations.pop();
	}
	
	public String getZoneDescription()
	{
		return currentLevel.description;
	}
}
